package com.arvato.minipro.controller.front.mall;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.minipro.bean.bean_do.mall.CommodityCartDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.mall.CommodityCartVo;
import com.arvato.minipro.bean.bean_vo.mall.EcCommodityVo;
import com.arvato.minipro.bean.bean_vo.mall.EcShoppingCartVo;
import com.arvato.minipro.service.mall.CommodityService;
import com.arvato.minipro.service.mall.ShoppingCartService;

/**
 * 
 * @author: smallking
 * 
 * @Descibe:购物车Controller
 * 
 * @Date: 2018年5月8日  上午11:24:50
 */
@Controller
@RequestMapping(value = "/mall/basket")
public class ShoppingCartController {
	
	@Autowired
	private ShoppingCartService shoppingCartService;
	
	@Autowired
	private CommodityService commodityService;
	
	/**
	 * 查询一个人的购物车商品列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.POST})
	public PageData list(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		String userId = req.getParameter("userId");
		if(userId == null) {
			return null;
		}
		Query query = new Query(param);
		// 如果method 是commodityInfo 就是从商品详情页创建订单的
		if("commodityInfo".equals(query.get("method"))) {
			String commodityId = (query.get("commodityId") + "") + "";
			List<CommodityCartVo> list = new ArrayList<CommodityCartVo>();
			CommodityCartVo commodityCartVo = new CommodityCartVo();
			commodityCartVo.setCommodityId(commodityId);
			commodityCartVo.setCount(1);
			commodityCartVo.setCommodityVo(commodityService.getCommodityById(commodityId));
			list.add(commodityCartVo);
			PageData pageData = new PageData(Long.valueOf("1"), list);
			pageData.setFront(true);
			return pageData;
		}
		// 如果method 是cut 就是从砍价界面跳转过来的
		if("cut".equals(query.get("method"))) {
			String commodityId = (query.get("commodityId")) + "";
			Float cutPrice = Float.valueOf(query.get("cutPrice") + "");
			List<CommodityCartVo> list = new ArrayList<CommodityCartVo>();
			CommodityCartVo commodityCartVo = new CommodityCartVo();
			commodityCartVo.setCommodityId(commodityId);
			commodityCartVo.setCount(1);
			EcCommodityVo commodityVo = commodityService.getCommodityById(commodityId);
			// 商品价格 = 原价 - 砍去的
			commodityVo.setPrice(commodityVo.getPrice() - cutPrice);
			commodityCartVo.setCommodityVo(commodityVo);
			list.add(commodityCartVo);
			PageData pageData = new PageData(Long.valueOf("1"), list);
			pageData.setFront(true);
			return pageData;
		}
		// 如果method 是cart 就是从购物车过来的
		if("cart".equals(query.get("method")) || query.get("method") == null) {
			if(query.get("commodityIds") != null) {
			    List<String> list = new ArrayList<String>();
				for(String commodityId : (query.get("commodityIds") + "").split(",")) {
					list.add(commodityId);
				}
				query.put("commodityIds", list);
			}
			query.put("userId", userId);
			PageData pageData = shoppingCartService.listCommodityCartByUserId(query);
			pageData.setFront(true);
			return pageData;
		}
		
		return null;
	}
	
	/**
	 * 将商品增加到购物车
	 * @param req
	 * @param commodityId
	 * @param count
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = {RequestMethod.POST})
	public Result insert(HttpServletRequest req, String commodityId, int count){
		Result result = new Result(false);
		String userId = req.getParameter("userId");
		if(userId == null) {
			return result;
		}
		// 将商品添加到购物车之前 要先检查当前的人是否有购物车
		EcShoppingCartVo cartVo = shoppingCartService.getShoppingCartByUserId(userId);
		if(cartVo == null) {
			return result;
		}
		CommodityCartDo commodityCartDo = new CommodityCartDo();
		commodityCartDo.setCommodityId(commodityId);
		commodityCartDo.setCount(count);
		commodityCartDo.setLastUpdateTime(new Date());
		commodityCartDo.setShoppingCartId(cartVo.getShoppingCartId());
		try {
			int updatedCount = shoppingCartService.insert(commodityCartDo);
			if(updatedCount != -500) {
				result.setResult(true);
				result.setData(updatedCount);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	
}
